<?php

include $_SERVER['DOCUMENT_ROOT'].'/inc/core/class/Template.php';
include $_SERVER['DOCUMENT_ROOT'].'/inc/core/class/Head.php';
include $_SERVER['DOCUMENT_ROOT'].'/inc/core/class/Body.php';

class Page{
	private $html = '';
	private $head = array();
	private $htmlTags = array();
	private $body = '';
	private $blocks = array();
	var $pId;
	private $path = '';
	private $label;
	private $name = '';
	
	function __construct($label = 'home', $id = null) {
		global $_DB;
			
		if (!empty($id)) {
			$sql = "SELECT id, name, label, parent FROM ".$_DB->getPrefix()."pages WHERE id = ".$id;
		} else {
			$sql = "SELECT id, name, label, parent FROM ".$_DB->getPrefix()."pages WHERE label = '".$label."'";
		}
		
		$p = $_DB->query($sql);
		if (!empty($p)) {
			$this->pId 	= $p[0]['id']; 
			$this->label= $p[0]['label'];
			$this->name	= $p[0]['name'];
		
			// load path of page
			$this->path = $this->pathTree($p[0]['parent']);
			
			//setting field head page
			$this->setHead($p[0]['id']);
			
			//load attribute page
			$this->setHtmlTags($p[0]);
			
			// load id blocks page
			$resBlocks = $this->blocksPage();
			foreach ($resBlocks as $i => $r) {
				$this->blocks[] = $r['id_block'];
			}
			
		} else {
			$description = 'page '.$_GET["path_str"].'/'.$label.'.html not found';
			$log = new Log('Page', 'Page.php', $description);
			$log->insert();
		}
		
		// load base html
		$tpl = new Template();
		$this->html = $tpl->html();
	}
	
	function get($field = null) {
		switch ($field) {
	    		case 'blocks':
	    			return $this->blocks;
	        	break;
	    		case 'path': 
	    			return $this->path;
	    		break;
	    		case 'htmlTags':
	    			return $this->htmlTags;
	    		break;
	    		case 'label':
	    			return $this->label;
	    		break;
	    		case 'name':
	    			return $this->name;
	    		break;
	    		case 'head':
	    			return $this->head;
	    		break;
	    		default: return null;
		}
	}
	
	function getUrl(){
		global $_SITE;
		return 'http://'.$_SITE["domain"].'/'.$this->path.$this->label.'.html';
	}
	
	function html() {
		return $this->html;
	}
	
	private function setHead($idPage) {
		global $_DB;
				
		$this->head["title"] = "";
		$this->head["description"] = "";
		$this->head["keywords"] = "";
		$this->head["author"] = "";
		
		$sql = "SELECT page.text, meta.name	
				FROM ".$_DB->getPrefix()."page_metatag as page, ".$_DB->getPrefix()."metatag as meta 
				WHERE page.id_page = '".$idPage."' AND meta.id = page.id_meta";
		
		$res = $_DB->query($sql);
		foreach ($res as $meta) {
			$this->head[$meta["name"]] = $meta["text"];
		}
	}
	
	private function setHtmlTags($page) {
		global $_DB;
		
		$sql = "SELECT * FROM ".$_DB->getPrefix()."page_attributes WHERE id_page = '".$this->pId."'";
		$res = $_DB->query($sql);
		foreach ($res as $attr) {
			$this->htmlTags[$attr["type"]] = $attr["value"];
		}
		
		$this->htmlTags["h1"] = $page["name"];
	}
	
	private function blocksPage() {
		global $_DB;
		
		$sql = "SELECT id_block FROM ".$_DB->getPrefix()."blocks_page WHERE id_page = '".$this->pId."' ORDER BY sort";
		return $_DB->query($sql);
	}
	
	function insertHead(){
		global $_TAG_REPLACE;

		$headHtml = new Head();
		
		$headHtml->setTitle($this->head["title"]);
		$headHtml->setDescription($this->head["description"]);
		$headHtml->setKeywords($this->head["keywords"]);
		$headHtml->setAuthor($this->head["author"]);
		
		$this->html = str_replace($_TAG_REPLACE["head_tags"], $headHtml->html(), $this->html);
	}
	
	function insertBody() {
		global $_TAG_REPLACE;

		$bodyHtml = new Body();
		$this->html = str_replace($_TAG_REPLACE["body_tags"], $bodyHtml->html(), $this->html);
	}
	
	function insertBlock($block) {
		$this->html = str_replace('<!-- '.$block->get('name').' //-->', $block->html(), $this->html());
	}

	function insertAdmin() {
		global $_ADMIN, $_TAG_REPLACE;
		$this->html = str_replace($_TAG_REPLACE["administrator"], $_ADMIN->html(), $this->html());
	}
	
	function insertJs() {
		global $_SITE, $_USER, $_TAG_REPLACE;
		
		$jsVars  = "\n";
		$jsVars .= '<script type="text/javascript">'."\n";
		$jsVars .= 'var idPage = "'.$this->pId.'"'.";\n";
		$jsVars .= 'var labelPage = "'.$_GET['label'].'"'.";\n";
		
		if ($_USER->get('id') != 0) {
			$jsVars .= 'var idUser = "'.$_USER->get('id').'"'.";\n";
		}
		
		$jsVars .= "</script>\n";
		$jsLib = '';
		foreach ($_SITE['js'] as $js) {
			$jsLib .= '<script type="text/javascript" src="'.$js.'" charset="utf-8"></script>'."\n";
		}
		$this->html = str_replace($_TAG_REPLACE["body_js"], $jsVars.$jsLib, $this->html());
	}
	
	function insertDebug($content) {
		global $_TAG_REPLACE;
		$this->html = str_replace($_TAG_REPLACE["debug"], $content, $this->html);
	}
	
	function removeDivDebug() {
		global $_TAG_REPLACE;
		$this->html = str_replace('<div class="debug">'.$_TAG_REPLACE["debug"].'</div>', '', $this->html);
	}
	
	function clearTagBlocks() {
		global $_TAG_REPLACE;
		foreach ($_TAG_REPLACE as $tag) {
			$this->html = str_replace($tag, '', $this->html);
		}
	}
	
	private function pathTree($pageId, &$pathTree = null) {
		global $_DB;
		
		if ($pageId == 0 || $pageId == -1) {
			$pathTree = $pathTree;
		} else {
			$sql = "SELECT label, parent FROM ".$_DB->getPrefix()."pages WHERE id = '".$pageId."' LIMIT 1";
			$page = $_DB->query($sql);
			
			$pathTree = $page[0]["label"].'/'.$pathTree;
			$this->pathTree($page[0]["parent"], $pathTree);
		}
		
		return $pathTree;
	}
}
?>